import json

from com.maycur.gateway.common.base_entity import ApiTaskSet, SequentialApiTaskSet
from locust import task, tag

from com.maycur.utils import TimeUtils


class Role(SequentialApiTaskSet):
    role_code: str

    @task
    def add_role(self):
        timestamp = TimeUtils.current_timestamp()
        role_add_url = f'{self.get_app_host()}/api//web/authz/v2/role'
        request_body = {
            "customizeLevelCode": 3,
            "roleName": f'{timestamp}',
            "permCodes": [
                "MANAGE_TRADING_PARTNER",
                "MAINTAIN_OPTION_GROUP",
                "EXCHANGE_RATE",
                "ORGANIZATION_MANAGE",
                "EMPLOYEE_MANAGE",
                "ROLE_AND_PERM",
                "MANAGE_USER_GROUP",
                "MANAGE_LEGAL_ENTITY",
                "MANAGE_PAYMENT_ACCOUNT",
                "MANAGE_CASCADE_RELATION"
            ],
            "entCode": "EC38QBKJ2FGZS0"
        }
        json_body = json.dumps(request_body)
        with self.client.post(role_add_url, data=json_body, headers=self.get_headers_with_content_type(),
                              march_response=True) as res:
            result_dto = self.parse_response(res)
            if result_dto is None:
                return
            self.role_code = result_dto.data['roleCode']

    @task
    def delete_role(self):
        role_delete_url = f'{self.get_app_host()}/api/web/authz/v2/role/{self.role_code}'
        with self.client.delete(role_delete_url, headers=self.get_headers_with_content_type(),
                                march_response=True) as res:
            self.parse_response(res)

    @task
    def get_role(self):
        role_get_url = f'{self.get_app_host()}/api/web/authz/v2/role/RI2206092026L1C0'
        with self.client.get(role_get_url, headers=self.get_headers_with_content_type(), march_response=True) as res:
            self.parse_response(res)
